package com.zmx.core.client.auth;

import com.zmx.core.dto.ChannelInfo;
import com.zmx.core.utils.AESUtil;
import com.zmx.core.utils.HostUtil;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 定时发送认证
 * @ClassName: HeartTask
 * @Author zhaomxr
 * @Date 2021-12-14 15:51
 */
@Slf4j
public class AgainRegisterTask implements Runnable {

    private String ip;
    private int port;
    private String username;
    private String password;
    private String secret;

    //持有引用,方便读写操做
    private ChannelHandlerContext ctx;

    public AgainRegisterTask(ChannelHandlerContext ctx, String ip, int port, String username, String password, String secret) {
        this.ctx = ctx;
        this.port = port;
        this.ip = HostUtil.IP;
        this.username = username;
        this.password = password;
        this.secret = secret;
    }

    @Override
    public void run() {
        try {
            Map<String, String> data = new HashMap<String, String>(2);
            data.put("host", ip + ":" + port);
            data.put("secret", AESUtil.aesEncrypt(username + password, secret));
            log.info("服务初始化认证：{}", data);

            ChannelInfo channelInfoR = new ChannelInfo("auth", data);
            ctx.writeAndFlush(channelInfoR);
        } catch (Exception e) {
            log.info("auth to server {} fail!");
        }
    }
}
